import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入login组件
// import login from '../components/login.vue'
const login = () => import(/* webpackChunkName: "login" */ '../components/login.vue')
// 导入Home组件
// import home from '../components/Home.vue'
const home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
// 导入欢迎页面
// import homeWelcome from '../components/Home_welcome.vue'
const homeWelcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home_welcome.vue')

// 用户列表区域
// import users from '../components/user/H_users.vue'
const users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/H_users.vue')
// 权限列表组件
// import rights from '../components/power/H_rights.vue'
const rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/H_rights.vue')
// 角色列表组件
// import roles from '../components/power/H_roles.vue'
const roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/H_roles.vue')

// 商品_商品分类组件
// import goodsCate from '../components/goods/H_cate.vue'
const goodsCate = () => import(/* webpackChunkName: "goodsCate_cateParams" */ '../components/goods/H_cate.vue')
// 商品_商品分类_分类参数组件
// import cateParams from '../components/goods/H_cate_params.vue'
const cateParams = () => import(/* webpackChunkName: "goodsCate_cateParams" */ '../components/goods/H_cate_params.vue')

// 商品列表
// import goodList from '../components/goods/H_goodsList.vue'
const goodList = () => import(/* webpackChunkName: "goodList_addGoods" */ '../components/goods/H_goodsList.vue')
// 添加商品
// import addGoods from '../components/goods/goods_add.vue'
const addGoods = () => import(/* webpackChunkName: "goodList_addGoods" */ '../components/goods/goods_add.vue')

// 订单列表
// import orderList from '../components/orders/orders_list.vue'
const orderList = () => import(/* webpackChunkName: "orderList_dataReports" */ '../components/orders/orders_list.vue')
// 数据报表
// import dataReports from '../components/report/data_reports.vue'
const dataReports = () => import(/* webpackChunkName: "orderList_dataReports" */ '../components/report/data_reports.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: homeWelcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: goodsCate },
      { path: '/params', component: cateParams },
      { path: '/goods', component: goodList },
      { path: '/goods/add', component: addGoods },
      { path: '/orders', component: orderList },
      { path: '/reports', component: dataReports }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 为路由挂载路由导航守卫函数
// next表示放行函数next() ,next('path')
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const skyToken = sessionStorage.getItem('token')
  if (!skyToken) return next('/login')
  next()
})

export default router
